<?php 
Yii::import('zii.widgets');

/**
 * Render a column with input can be saved by posting values in array
 * @version 1.0.0
 * @author Nguyen Quang Nghia
 *
 **/
/**
 * This version render input and allow saving value in a simple way 
 * @var string
 * 
**/

class NSortColumn extends CDataColumn
{
	/** the url of aciton where trigger will be executed
	* a URL or an action route that can be used to create a URL. See normalizeUrl for more details about how to specify this parameter.
	*/
	public $url = array('reOrder');

	/** 
	* Id will be assigned to save button in header
	**/
	private $triggerLinkId;

	// public $groupAttr = 'visible';

	public function init()
	{
		parent::init();
		
		// building url string
		$this->url = CHtml::normalizeUrl($this->url);

		$this->triggerLinkId = $this->id . '-trigger';
		$this->headerHtmlOptions = CMap::mergeArray($this->headerHtmlOptions,array('width'=>'55'));
	}

	/**
	* Renders the header cell content.
	* This method will render a link that can trigger the sorting if the column is sortable.
	*/
	protected function renderHeaderCellContent()
	{
		if($this->grid->enableSorting && $this->sortable && $this->name!==null)
			echo $this->grid->dataProvider->getSort()->link($this->name,$this->header);
		else if($this->name!==null && $this->header===null)
		{
			if($this->grid->dataProvider instanceof CActiveDataProvider)
				echo CHtml::encode($this->grid->dataProvider->model->getAttributeLabel($this->name));
			else
			echo CHtml::encode($this->name);
		}
		else
			parent::renderHeaderCellContent();
		echo CHtml::link(NHtml::themeImage('images/icons/save.gif'), 'javascript:void(0)', array('class'=>"mls", 'id'=> $this->triggerLinkId,'style'=>'vertical-align:middle;'));
	}


	/**
	* Renders the data cell content.
	* This method evaluates {@link value} or {@link name} and renders the result.
	* @param integer $row the row number (zero-based)
	* @param mixed $data the data associated with the row
	*/
	protected function renderDataCellContent($row,$data)
	{
		// refine value entered
		$value=Yii::app()->format->formatNumber(CHtml::value($data, $this->name, 0));

		cs()->registerScript($this->id . '-script', "$('body').on('click', '#{$this->triggerLinkId}', function(){
			\$.ajax({'async':false,'cache':false,
				'context':$(this),
				'type':'POST',
				'url':'{$this->url}',
				'data':$('input[name*=\"multisort\"]').serialize(),
				'success':function(data){
					data = $.parseJSON(data);
					if(data['result']=='error') alert(data['message']);
					else if(data['result']=='success')
					{
						if(data['effected_rows']>0)
							\$.fn.yiiGridView.update(\"{$this->grid->id}\");
					}
						
				},
				'error':function(data){
					alert(data.responseText);
				}
			});
			return false;
		});");

		$output = CHtml::textField("multisort[{$data->id}]", $value, array('class'=>'alc', 'size'=>3));
		echo CHtml::tag('div',array('class'=>'alc'), $output);
	}


	private function nextVersion()
	{
		/** TODO
		1. allow other type to be updated: string 
		2. allow grouping
		// Below codes will be used in later version to allow more flexible in grouping sorting value
		// $groupValue = CHtml::value($data, $this->groupAttr, NULL);

		// echo CHtml::link("[Up]", '#', array('class'=>'mrs'));
		// echo CHtml::link("[Down]", '#', array('class'=>'mrs'));

		// $output = CHtml::textField("multisort[$groupValue][{$data->id}]", $value, array('class'=>'alc', 'size'=>3));
		*/
	}

}